package org.example.service.impl;

import org.example.dao.DepartmentDao;
import org.example.dto.DepartmentDto;
import org.example.pojo.Department;
import org.example.service.DepartmentService;
import org.example.vo.DepartmentVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Resource
    private DepartmentDao dao;

    private DepartmentVo PoTransToVo(Department department){
        DepartmentVo department1=new DepartmentVo();
        department1.setDepartmentName(department.getDepartname());
        department1.setDepartmentId(department.getDepartno());
        return department1;
    }

    private DepartmentDto PoTransToDto(Department department){
        DepartmentDto departmentDto=new DepartmentDto();
        departmentDto.setDepartmentId(department.getDepartno());
        departmentDto.setDepartmentName(department.getDepartname());
        return departmentDto;
    }
    private Department DtoTransToPo(DepartmentDto department){
        Department department1=new Department();
        department1.setDepartname(department.getDepartmentName());
        department1.setDepartno(department.getDepartmentId());
        return department1;
    }
    private Department VoTransToPo(DepartmentVo department){
        Department department1=new Department();
        department1.setDepartname(department.getDepartmentName());
        department1.setDepartno(department.getDepartmentId());
        return department1;
    }


    @Override
    public Integer addDepartment(DepartmentVo department) {
        if(dao.insert(VoTransToPo(department)))
            return 1;
        else
            return 0;
    }

    @Override
    public Integer updateDepartment(DepartmentVo department) {
        if(dao.update(VoTransToPo(department)))
            return 1;
        else
            return 0;
    }

    @Override
    public Integer deleteDepartment(Integer departmentId) {
        if(dao.delete(departmentId))
            return 1;
        else
            return 0;
    }

    @Override
    public List<DepartmentDto> queryAllDepartment() {
        List<Department> departmentList=dao.queryAll();
        List<DepartmentDto> departmentDtoList=new ArrayList<>();
        for(int i=0;i<departmentList.size();i++)
            departmentDtoList.add(i,PoTransToDto(departmentList.get(i)));
        return departmentDtoList;
    }

    @Override
    public Integer countDepartment() {
        return dao.queryAll().size();
    }

    @Override
    public DepartmentDto queryDepartmentByName(String departmentName) {
        Department department=new Department();
        department.setDepartname(departmentName);
        return PoTransToDto(dao.queryByCondition(department).get(0));
    }

    @Override
    public DepartmentDto queryDepartmentById(Integer departmentId) {
        Department department=new Department();
        department.setDepartno(departmentId);
        return PoTransToDto(dao.queryByCondition(department).get(0));
    }
}
